<script setup>
import {onMounted, ref, reactive, defineEmits} from 'vue'
import {saveApi, updateApi} from "../../../api/canteenguest";
const emit = defineEmits(['flush']);
// 表单数据
const formData = ref({
  id: 0,
  name:'',
  eatDate: '',
  type: '',
  createTime:''
})

const clearFormData = () =>
{
  formData.value = {
    id: 0,
    name:'',
    eatDate: '',
    type: '',
    createTime:''
  }
}

//自定义输入框的数量
const contentLength=ref(1);
const isupdate=ref(false)

// 表单验证
const formRules = reactive({
  title: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 6, message: '请输入2~6个字符', trigger: 'blur' }
  ]
})

// 提交验证
const formRef = ref()
const onSubmit = () => {
  formRef.value.validate(async (isSuccess) => {
    if (isSuccess) {
      console.log(formData.value)
      let r;
      if(isupdate.value){
        r=await updateApi(formData.value)
      }else {
        r=await saveApi(formData.value)
      }
      if(r.code==0){
        ElMessage.success(isupdate.value?"修改人员用餐信息成功":"新增人员用餐信息成功")
        emit("flush")
        state.value=false
      }else {
        ElMessage.error(r.data)
      }
    }
  })
}


// 对话框状态
const state = ref(false)
const value = ref('')
const options = [
  {
    value: '早餐',
    label: '早餐',
  },
  {
    value: '午餐',
    label: '午餐',
  },
  {
    value: '晚餐',
    label: '晚餐',
  },

]
// 暴露
defineExpose({
  state,
  formData,
  isupdate
})
</script>
<template>
  <el-dialog
    v-model="state"
    :title="`${isupdate ? '修改' : '添加'} 人员用餐信息`"
    width="600"
    :before-close="() => (state = false)"
  >
    <!-- 内容 -->
    <el-form
      v-if="state"
      size="large"
      label-width="100"
      :model="formData"
      :rules="formRules"
      ref="formRef"
    >
      <el-form-item label="人员信息">
        <el-input placeholder="请输入人员姓名" v-model="formData.name" style="width: 180px"/>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="用餐日期">
          <el-date-picker
              v-model="formData.eatDate"
              value-format="YYYY-MM-DD"
              type="date"
              placeholder="请选择日期"
          />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="用餐类型" >
        <el-select v-model="formData.type" placeholder="请选择用餐类型" style="width: 180px">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="default" @click="state = false">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 内容 -->
  </el-dialog>
</template>
